//
//  JobDetailAttachmentView.swift
//  GreenDeal
//
//  Created by Moennig Q on 27/12/2022.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
enum JobDetailAttachmentType: Int{
    case OtherAttachment = 1
}
struct JobDetailAttachmentModel {
    var  title: String = ""
    var  subTitle: String = ""
    var  attachmentType: JobDetailAttachmentType  = .OtherAttachment
}
class JobDetailAttachmentView: CustomBackView {
    fileprivate lazy var disposeBag = DisposeBag()
    var dataObserver: BehaviorRelay<[JobDetailAttachmentModel]> = BehaviorRelay(value: [])
    let tableView = UITableView()
    //
    var tableViewHeightConstraint: Constraint?
    override func setUpView() {
        self.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            self.tableViewHeightConstraint = make.height.greaterThanOrEqualTo(50).constraint
            make.bottom.equalTo(-8)
        }
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.register(JobDetailAttachmentTableViewCell.self, forCellReuseIdentifier: JobDetailAttachmentTableViewCell.reuseCellIdentifier)
       
        dataObserver.bind(to: tableView.rx.items(cellIdentifier: JobDetailAttachmentTableViewCell.reuseCellIdentifier,cellType: JobDetailAttachmentTableViewCell.self)){(row, model, cell) in
            cell.setData(model)
        }.disposed(by: self.disposeBag)
        //监听属性变化
        tableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.tableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //
        self.setData()
    }
    private func setData(){
        var list = [JobDetailAttachmentModel]()
        //other attachment
        var m = JobDetailAttachmentModel()
        m.title = "Other Documents"
        m.subTitle = "Eg. JSA, invoices, reports, etc"
        m.attachmentType = JobDetailAttachmentType.OtherAttachment
        list.append(m)
        dataObserver.accept(list)
    }
}
